#!/usr/bin/python3
# -*- coding: utf-8 -*-

import re
import numpy as np
import pdfplumber
import tabula
import pandas as pd
from utils import *


def load_pdfplumber_tables(path):
    """
    解析PDF的表格数据并进行数据预处理

    Parameters
    ----------
    path

    Returns
    -------

    """
    with pdfplumber.open(path) as pdf:
        tables = []
        for page in pdf.pages:
            for table in page.extract_tables():
                for field in np.array(table).flatten():
                    if field:
                        tables += list(
                            filter(
                                lambda x: x and "cid" not in x,
                                re.split(r':|：',
                                         field
                                         .replace("\n", "")
                                         .replace(" ", "")
                                         .replace("\xa0", "")
                                         .replace("\u3000", "")
                                         )
                            )
                        )
    return tables


def load_pdfplumber_words(path):
    """
    解析PDF的表格数据并进行数据预处理

    Parameters
    ----------
    path

    Returns
    -------

    """
    from shapely.geometry import box
    from matplotlib.pylab import plt
    with pdfplumber.open(path) as pdf:
        words = []
        for page in pdf.pages:
            for words in page.extract_words(x_tolerance=3, y_tolerance=3):
                print(words)
                region = box(minx=words["x0"], miny=words["bottom"], maxx=words["x1"], maxy=words["top"])
                print(region)
                plt.plot(*region.exterior.xy)
            plt.show()

    return words


def load_pdfplumber_texts(path):
    """
    解析PDF的文本数据并进行数据预处理

    Parameters
    ----------
    path

    Returns
    -------

    """
    with pdfplumber.open(path) as pdf:
        texts = []
        for page in pdf.pages:
            text = page.extract_text()
            if text:
                for t in re.split(r' |\n|:|：', text.replace("\xa0", "")):
                    match_t = matching_chinese_alpha_digit_special(t)
                    if match_t and "cid" not in match_t:
                        texts.append(match_t)

                # texts += list(filter(lambda x: x and "cid" not in x, re.split(r' |\n|:|：', text.replace("\xa0", ""))))
                # texts += list(
                #     filter(
                #         lambda x: x and "cid" not in x,
                #         re.split(r':|：', text.replace("\n", "").replace(" ", "").replace("\xa0", ""))
                #     )
                # )
    return texts


def load_tabula_tables(path):
    """
    解析PDF的表格数据并进行数据预处理

    Parameters
    ----------
    path

    Returns
    -------

    """

    tables = []
    for table in tabula.read_pdf(path, encoding='utf-8', pages='all'):
        table = table.values.flatten()
        for t in table:
            if not pd.isna(t):
                tables += list(filter(lambda x: x, re.split(
                    r':|：',
                    str(t).replace("\r", "").replace("\n", "").replace(" ", "").replace("\xa0", "")
                )))
    return tables
